package com.limh.readers.activity

import android.app.AlertDialog
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.RadioGroup
import android.widget.TextView
import butterknife.BindView
import butterknife.OnClick
import com.limh.readers.R
import com.limh.readers.bean.User
import com.limh.readers.comm.Comm
import com.limh.readers.net.BaseRetrofit
import com.limh.readers.net.ServerApi
import com.limh.readers.utils.GsonUtils
import com.limh.readers.utils.LogUtil
import com.limh.readers.utils.SharePreferUtils
import com.limh.readers.widget.CircleTransform
import com.squareup.picasso.Picasso
import org.jsoup.helper.StringUtil
import rx.schedulers.Schedulers

/**
 * Function:
 * author: limh
 * time:2017/7/24
 */
class UserInfoActivity : BaseActivity() {
    @BindView(R.id.image_head)
    lateinit var imageHead: ImageView
    @BindView(R.id.txt_username)
    lateinit var txtName: TextView
    @BindView(R.id.txt_sign)
    lateinit var txtSign: TextView
    @BindView(R.id.image_sex)
    lateinit var imageSex:ImageView

    private var user: User? = null
    val TAG = "UserInfoActivity"
    override fun findView() {
        setContentView(R.layout.activity_user_info)
    }

    override fun initViews() {
        if ("" != SharePreferUtils.get(this, "user", "")) {
            user = GsonUtils.str2Bean(SharePreferUtils.get(this, "user", "") as String, User::class.java)
            LogUtil.i(TAG, "USER" + user.toString())
            txtName.text = user!!.username
            txtSign.text = user!!.sign
            when (user!!.sex) {
                "boy" -> Picasso.with(this).load(R.drawable.ic_boy).into(imageSex)
                "girl" -> Picasso.with(this).load(R.drawable.ic_girl).into(imageSex)
            }

        }
        Picasso.with(this).load(R.drawable.ic_my_avatar)
                .transform(CircleTransform()).into(imageHead)
    }

    override fun initDatas() {
    }

    @OnClick(R.id.image_close, R.id.txt_sign, R.id.image_sex)
    fun OnClick(view: View) {
        when (view.id) {
            R.id.image_close -> finish()
            R.id.txt_sign -> showSetSign()
            R.id.image_sex -> showSetSex()
        }
    }

    /**
     * 请设置个性签名
     */
    fun showSetSign() {
        val view = LayoutInflater.from(this).inflate(R.layout.view_dialog_edit, null)
        val builder = AlertDialog.Builder(this)
                .setView(view)
                .create()
        val edit1 = view.findViewById<EditText>(R.id.edit_1)
        edit1.hint = "请设置个性签名"
        val edit2 = view.findViewById<EditText>(R.id.edit_2)
        edit2.visibility = View.GONE
        val btn1 = view.findViewById<TextView>(R.id.txt_btn_1)
        btn1.setOnClickListener { if (builder.isShowing) builder.dismiss() }
        val btn2 = view.findViewById<TextView>(R.id.txt_btn_2)
        btn2.setOnClickListener {
            netSync("sign", edit1.text.toString())
            user!!.sign = edit1.text.toString()
            if (builder.isShowing) builder.dismiss()
        }
        builder.show()
    }

    /**
     * 设置性别
     */
    fun showSetSex() {
        val view = LayoutInflater.from(this).inflate(R.layout.view_dialog_sex, null)
        val builder = AlertDialog.Builder(this)
                .setView(view)
                .create()
        val radio = view.findViewById<RadioGroup>(R.id.radio)
        when (user!!.sex) {
            "boy" -> radio.check(R.id.radio_boy)
            "girl" -> radio.check(R.id.radio_girl)
        }
        val btn1 = view.findViewById<TextView>(R.id.txt_btn_1)
        btn1.setOnClickListener { if (builder.isShowing) builder.dismiss() }
        val btn2 = view.findViewById<TextView>(R.id.txt_btn_2)
        btn2.setOnClickListener {
            val checkId = radio.checkedRadioButtonId
            var value = "boy"
            when (checkId) {
                R.id.radio_boy -> value = "boy"
                R.id.radio_girl -> value = "girl"
            }
            netSync("sex", value)
            if (builder.isShowing) builder.dismiss()
        }
        builder.show()
    }

    /**
     * 设置信息同步到服务器
     */
    fun netSync(field: String, value: String) {
        if (StringUtil.isBlank(value)) {
            showToastShort("输入为空")
            return
        }
        val params = mapOf(Pair("token", Comm.TOKEN), Pair(field, value))
        val service = BaseRetrofit().createService(ServerApi::class.java)
        service.update(params)
                .subscribeOn(Schedulers.newThread())
                .subscribeOn(Schedulers.io())
                .subscribe({ reponse ->
                    when (reponse.getCode()) {
                        200 -> sendMsg("设置成功", 0x123, handler)
                        else -> sendMsg(reponse.getMessage(), 0x123, handler)
                    }
                }, { t ->
                    LogUtil.e(TAG, "异常：" + t.message)
                })
    }

    private val handler = Handler(Handler.Callback { msg ->
        when (msg.what) {
            0x123 -> {
                txtSign.text = user!!.sign
                when (user!!.sex) {
                    "boy" -> Picasso.with(this).load(R.drawable.ic_boy).into(imageSex)
                    "girl" -> Picasso.with(this).load(R.drawable.ic_girl).into(imageSex)
                }
                SharePreferUtils.put(this, "user", GsonUtils.Gson2Str(user))
                showToastShort(msg.data.getString("msg"))
            }
        }
        false
    })
}